package com.tq.tuqupro;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import com.tq.R;
import com.tq.custom.MyGridView;
import com.tq.custom.MyListView;
import com.tq.custom.MyScrollView;
import com.tq.custom.MyScrollView.OnMoreListener;
import com.tq.custom.MyScrollView.OnRefreshListener;
import com.tq.http.ImageLoader;
import com.tq.http.TBaseHttpClient;
import com.tq.model.Result;
import com.tq.model.User;

import com.tq.util.JsonUtil;
import com.tuqu.asynctask.AsyncTaskListener;
import com.tuqu.asynctask.TAsyncTask;

import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends TBaseActivity{


	private Button btn;
	private EditText et,et2;
	private ImageButton ib,ib2;
	private TAsyncTask  task1;
	private List<User> userList;
	private ImageView iv;
	private MyScrollView scrollView;
	private MyGridView lv;
	private LinearLayout headview,footview;
	private MyHandler myHandler;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		myHandler=new MyHandler();
//		btn=initBtn(R.id.btn);
//		iv=initIv(R.id.iv_pic);
//		userList=new ArrayList<User>();
//		
//		
//		et=initEt(R.id.et_lgphone);
//		ib=initIb(R.id.ib_lgclear1);
//		
//		et2=initEt(R.id.et_lgpsw);
//		ib2=initIb(R.id.ib_lgclear2);
//		
//		//ibChangeText(et, ib);
//		//ibChangeText(et2, ib2);
//		
//		btn.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				
//				//task1.cancel(true);
//				task1=new TAsyncTask(MainActivity.this);
//				task1.setTaskID(100001);
//				if(isInternet())
//				{
//					task1.execute("15886789887","514297");
//				}
//				else
//				{
//					showToast(0,"无网络连接");
//				}
//				
//				new ImageLoader().download("http://192.168.1.106:8080/nyist/wojj.png", iv);
//				
//			}
//		});
		
		scrollView=(MyScrollView)findViewById(R.id.myscroll);
		scrollView.setonRefreshListener(new OnRefreshListener() {
			
			@Override
			public void onRefresh() {
				// TODO Auto-generated method stub
				
			}
		});
		scrollView.setonMoreListener(new OnMoreListener() {
			
			@Override
			public void onMore() {
				// TODO Auto-generated method stub
//				Timer timer = new Timer();
//				TimerTask task = new TimerTask() {
//
//					@Override
//					public void run() {
//						// TODO Auto-generated method stub
//						scrollView.onMoreComplete();
//					}
//				};
//
//				timer.schedule(task, 3 * 1000);
				
				System.out.println("onmore***************");
				
				myHandler.sendEmptyMessageDelayed(0,30000);
				
			}
		});
		headview=(LinearLayout)findViewById(R.id.headview);
		
		footview=(LinearLayout)findViewById(R.id.footview);
		headview.addView(scrollView.getViewHeader());
		footview.addView(scrollView.getViewFooter());
		
		lv=(MyGridView)findViewById(R.id.gv_custom);
		lv.setAdapter(new BaseAdapter() {
			
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				// TODO Auto-generated method stub
				TextView tv=new TextView(MainActivity.this);
				tv.setText("test");
				return tv;
			}
			
			@Override
			public long getItemId(int position) {
				// TODO Auto-generated method stub
				return 0;
			}
			
			@Override
			public Object getItem(int position) {
				// TODO Auto-generated method stub
				return null;
			}
			
			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return 25;
			}
		});
		

		
	}
	
	private class MyHandler  extends Handler
	{

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case 0:
				scrollView.onMoreComplete();
				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}
		
	}
	
	@Override
	public int setLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.main;
	}
	@Override
	public void onBeforeTask(int taskId) {
		// TODO Auto-generated method stub
		if(taskId==100001)
		{
			System.out.println("onBeforeTask");
			showPgDlg("登录","正在验证登陆信息...",R.drawable.icon_32);
		}
		
		
	}
	@Override
	public Object onTask(int taskId, Object... params) throws Exception {
		// TODO Auto-generated method stub
		HashMap<String, Object> map=new HashMap<String, Object>();
		map.put("phone",params[0]);
		map.put("pwd",params[1]);
		String result=httpClient().post("http://192.168.1.100:2222/youyu/androiduser/userLogin.action",map);
		return result;
	}
	@Override
	public void onAfterTask(int taskId, Object result) {
		// TODO Auto-generated method stub
		if(taskId==100001)
		{

			User user=JsonUtil.fromJson(result.toString(),User.class);
			
		
//			userList.add(user);
//			
//			saveAllPref("UserInfo",userList,"user_phone","user_psw");
			
			closePgDlg();
			
			
			
		}
		
	}
	@Override
	public void onTaskError(int taskId, Exception exception) {
		// TODO Auto-generated method stub
	
		if(taskId==100001)
		{
			showToast(0,"登录出错");
			closePgDlg();
			
			
		}
	}

//	private class MyHandler extends Handler
//	{
//
//		@Override
//		public void handleMessage(Message msg) {
//			// TODO Auto-generated method stub
//			switch (msg.what) {
//			case 0:
//				showToast(0,"正在下载");
//				try {
//					Thread.sleep(3000);
//				} catch (InterruptedException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//				
//				break;
//
//			default:
//				break;
//			}
//			super.handleMessage(msg);
//		}
//		
//	}
	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_DOWN)
		{
//			
//			if(task1!=null&&task1.getStatus()==Status.RUNNING)
//			{
//				
//				task1.cancel(true);
//				closePgDlg();
//			}
//			else 
//			{
//				finish();
//			}
			
			
		}
		return super.onKeyDown(keyCode, event);
	}
	
	
	
	
	
	
	
}
